<template>
  <div class="app-container">
     <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="openSelectUser"
          v-hasPermi="['system:role:add']"
        >添加用户</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-circle-close"
          size="mini"
          :disabled="multiple"
          @click="delAuthUserBatch"
          v-hasPermi="['system:role:remove']"
        >批量取消角色</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-close"
          size="mini"
          @click="handleClose"
        >关闭</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange" row-key="userId">
      <el-table-column type="selection" :reserve-selection="true" width="55" align="center" />
      <el-table-column label="分配角色名称" prop="roleName" :show-overflow-tooltip="true" align="center"/>
      <el-table-column label="关联用户" prop="userName" :show-overflow-tooltip="true" align="center"/>
      <el-table-column label="分配时间" align="center" prop="createTime" width="180"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            @click="delAuthUser(scope.row)"
            v-hasPermi="['system:role:remove']"
          >取消用户角色</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />
    <select-user v-if="selectUserData.open" :prop="selectUserData" @close="closeSelectUser"/>
  </div>
</template>

<script>
import { allocatedUserList, delRoleUsers } from "@/api/system/role";
import selectUser from "./selectUser";

export default {
  name: "AuthUser",
  components: { selectUser },
  data() {
    return {
      loading: true,
      roleUserIds: [],
      multiple: true,
      showSearch: true,
      total: 0,
      userList: [],
      selectUserData:{
        open:false,
        roleId:null,
      },
      queryParams: {
        current: 1,
        size: 10,
        roleId: null,
        userName: null,
      }
    };
  },
  created() {
    const roleId = this.$route.params && this.$route.params.roleId;
    if (roleId) {
      this.queryParams.roleId = roleId;
      this.getList();
    }
  },
  methods: {
    /** 查询角色用户列表 */
    getList() {
      this.loading = true;
      allocatedUserList(this.queryParams).then(res => {
          this.userList = res.data.records;
          this.total = Number.parseInt(res.total);
          this.loading = false;
        }
      );
    },
    // 返回按钮
    handleClose() {
      const obj = { path: "/system/role" };
      this.$tab.closeOpenPage(obj);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.roleUserIds = selection.map(item => item.roleUserId)
      this.multiple = !selection.length
    },
    openSelectUser() {
      this.selectUserData = {open:true,roleId:this.queryParams.roleId}
    },
    closeSelectUser(saveParams){
      this.selectUserData = {open:false,roleId:null}
      if(saveParams.isSaveData){
        this.getList()
      }
    },
    /** 取消角色按钮操作 */
    delAuthUser(row) {
      this.$confirm('是否取消该用户角色关联？','确认信息',{
        distinguishCancelAndClose: true,
        confirmButtonText: '确认',
        cancelButtonText: '取消'
      }).then(()=>{
        console.log('问题')
        delRoleUsers([row.roleUserId]).then(res =>{
          if(res.code === 200){
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            this.getList()
          }
        })
      }).catch(action => {

      });
    },
    /** 批量取消角色按钮操作 */
    delAuthUserBatch() {
      if(this.roleUserIds!==null && this.roleUserIds.length>0){
        this.$confirm('是否取消已选中用户角色关联？','确认信息',{
          distinguishCancelAndClose: true,
          confirmButtonText: '确认',
          cancelButtonText: '取消'
        }).then(()=>{
          delRoleUsers(this.roleUserIds).then(res =>{
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            this.roleUserIds = null
            this.getList()
          })
        }).catch(action => {

        });
      }
    }
  }
};
</script>
